package com.kehutong.work.order.manage.mini;

import javax.servlet.http.HttpServletRequest;

import org.coraframework.inject.Inject;
import org.coraframework.json.JSONObject;
import org.coraframework.mvc.MvcAction;
import org.coraframework.mvc.http.Bean;
import org.coraframework.mvc.http.ReqMapping;
import org.coraframework.util.Objects;

import com.kehutong.common.TokenService;
import com.kehutong.common.errcode.Result;
import com.kehutong.common.util.Token;
import com.kehutong.work.order.manage.entity.WorkOrderManageAppraisal;
import com.kehutong.work.order.manage.service.WorkOrderAppraisalService;

/**
 * 小程序-工单管理-评论相关
 */
@MvcAction
@ReqMapping("/kht-bin/mini/work/order/manage/appraisal")
public class WorkOrderAppraisalAction {

    @Inject
    private WorkOrderAppraisalService appraisalService;
    @Inject
    private TokenService tokenService;

    @ReqMapping("/list/page")
    public Object listPage(JSONObject jsonObject) {

        jsonObject.put("userShow", "1");
        return appraisalService.listPage(jsonObject);
    }

    @Bean(newInstance=true, copy=true)
    @ReqMapping("/save")
    public Object save(HttpServletRequest req, WorkOrderManageAppraisal appraisal) throws Exception {
        Token token = tokenService.getUserToken(req);

        if (!checkLogin(token)) {
            return Result.error(0, "请先登录");
        }

        appraisal.setUserNo(token.getUuid());
        appraisal.setUserName(Objects.nonEmpty(appraisal.getUserName()) ? appraisal.getUserName() : token.getUsername());
        appraisal.setUserShow(true);
        appraisal.setUserCreate(true);

        return appraisalService.save(appraisal, token);
    }

    private boolean checkLogin(Token token){
        if (Objects.isNull(token)) {
            return false;
        }

        if (Objects.isEmpty(token.getUuid()) && Objects.isEmpty(token.getUsername())) {
            return false;
        } else {
            return true;
        }
    }

}
